package com.ruoyi.web.controller.applet;


import com.ruoyi.applet.product.domain.Product;
import com.ruoyi.applet.product.domain.ProductSpecifications;
import com.ruoyi.applet.product.service.IProductService;
import com.ruoyi.applet.product.service.IProductSpecificationsService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 产品规格Controller
 * @author ruoyi
 * @date 2024-11-20
 */
@Controller
@RequestMapping("/applet/productSpecifications")
public class ProductSpecificationsController extends BaseController {
    
    private final String prefix = "applet/productSpecifications";
    
    @Resource
    private IProductSpecificationsService productSpecificationsService;
    @Resource
    private IProductService productService;
    
    @RequiresPermissions("applet:product:view")
    @GetMapping("/{id}")
    public String productSpecifications(@PathVariable("id") Long id, ModelMap mmap) {
        mmap.put("productId", id);
        return prefix + "/productSpecifications";
    }
    
    /**
     * 查询产品规格列表
     */
    @RequiresPermissions("applet:product:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProductSpecifications productSpecifications) {
        startPage();
        List<ProductSpecifications> list = productSpecificationsService.selectProductSpecificationsList(productSpecifications);
        return getDataTable(list);
    }
    
    /**
     * 新增产品规格
     */
    @GetMapping("/add/{productId}")
    public String add(@PathVariable("productId") Long productId, ModelMap mmap) {
        Product product = productService.selectProductById(productId);
        mmap.put("product", product);
        return prefix + "/add";
    }
    
    /**
     * 新增保存产品规格
     */
    @RequiresPermissions("applet:product:add")
    @Log(title = "产品规格", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProductSpecifications productSpecifications) {
        return toAjax(productSpecificationsService.insertProductSpecifications(productSpecifications));
    }
    
    /**
     * 修改产品规格
     */
    @RequiresPermissions("applet:product:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        ProductSpecifications productSpecifications = productSpecificationsService.selectProductSpecificationsById(id);
        mmap.put("productSpecifications", productSpecifications);
        Product product = productService.selectProductById(productSpecifications.getProductId());
        mmap.put("product", product);
        return prefix + "/edit";
    }
    
    /**
     * 修改保存产品规格
     */
    @RequiresPermissions("applet:product:edit")
    @Log(title = "产品规格", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProductSpecifications productSpecifications) {
        return toAjax(productSpecificationsService.updateProductSpecifications(productSpecifications));
    }
    
    /**
     * 删除产品规格
     */
    @RequiresPermissions("applet:product:remove")
    @Log(title = "产品规格", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(productSpecificationsService.deleteProductSpecificationsByIds(ids));
    }
}
